﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using Data.AID;
using System.Linq;
using Business.AID;
using Business.AID.Extensions;
using System.Text;

namespace Presentation.AID.WebPart.Presentation.AID.VWPRelatorioRecursosHumanos
{
    public partial class VWPRelatorioRecursosHumanosUserControl : UserControl
    {
        private List<Formulario> ListaFormulario
        {
            get { return Cache["ListFormulario"] as List<Formulario>; }
            set { Cache["ListFormulario"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SetDropDownList();
                SetGridView();
            }
        }

        #region Métodos Privados

        private void SetDropDownList()
        {
            CategoriaBO categoriaBO = new CategoriaBO();
            List<Categoria> listCategory = categoriaBO.GetAllCategories();

            ddlEstagio.AppendDataBoundItems = true;
            ddlEstagio.Items.Add(new ListItem(" -- Todos -- ","0"));
            ddlEstagio.DataSource = listCategory.OrderBy(p => p.IdCategoria);
            ddlEstagio.DataTextField = "CategoriaDesc";
            ddlEstagio.DataValueField = "IdCategoria";
            ddlEstagio.DataBind();
            ddlEstagio.AppendDataBoundItems = false;
        }
        private void FilterReport()
        {
            string nome = txtNome.Text.RemoveAccents();
            string matricula = txtMatricula.Text;
            
            int numeroForm;
            int.TryParse(txtNumero.Text, out numeroForm);

            int estagio;
            int.TryParse(ddlEstagio.SelectedValue, out estagio);

            if (ListaFormulario != null)
            {
                ListaFormulario = GetAllForms();

                if (nome != "" || matricula != "" || numeroForm != 0 || estagio != 0)
                {
                    if (nome != "")
                    {
                        ListaFormulario = ListaFormulario.Where(p => p.Nome.ToLower().RemoveAccents().Contains(nome.ToLower())).ToList();
                    }

                    if (matricula != "")
                    {
                        ListaFormulario = ListaFormulario.Where(p => p.Matricula.ToLower().Contains(matricula.ToLower())).ToList();
                    }

                    if (numeroForm != 0)
                    {
                        ListaFormulario = ListaFormulario.Where(p => p.NumeroFormulario == numeroForm).ToList();
                    }

                    if (estagio != 0)
                    {
                        ListaFormulario = ListaFormulario.Where(p => p.Estagio == estagio).ToList();
                    }
                    gdvFormularios.DataSource = ListaFormulario;
                    gdvFormularios.DataBind();
                }
                else
                {
                    gdvFormularios.DataSource = ListaFormulario;
                    gdvFormularios.DataBind();
                }
            }
        }
        private List<Formulario> GetAllForms()
        {
            FormularioBO formularioBO = new FormularioBO();
            return formularioBO.GetAllForms().OrderBy(p => p.Nome).ToList();
        }
        private void SetGridView()
        {
            ListaFormulario = GetAllForms();
            gdvFormularios.DataSource = ListaFormulario;
            gdvFormularios.DataBind();
        }

        #endregion

        #region Eventos

        protected void gdvFormularios_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            int newPageIndex = e.NewPageIndex;
            List<Formulario> list = ListaFormulario;

            gdvFormularios.DataSource = list;
            gdvFormularios.PageIndex = newPageIndex;
            gdvFormularios.DataBind();
        }
        protected void gdvFormularios_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Historico" || e.CommandName == "Visualizar")
            {
                string cname = e.CommandName;

                var teste = e.CommandArgument;

                int rowIndex = Convert.ToInt32(e.CommandArgument);
                DataKey dtKey = gdvFormularios.DataKeys[rowIndex];
                int IdForm = Convert.ToInt32(dtKey["ID"]);

                if (e.CommandName == "Historico")
                {
                    Response.Redirect(@"/SitePages/Historico.aspx?idForm=" + IdForm + "&viewer=rh");
                }

                if (e.CommandName == "Visualizar")
                {
                    Response.Redirect(@"/SitePages/VisualizarFormulario.aspx?idForm=" + IdForm);
                }
            }
        }
        
        #endregion

        #region Botões

        protected void btnAnexar_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;

            GridViewRow row = (GridViewRow)button.NamingContainer;
            int rowIndex = row.RowIndex;
            DataKey dtKey = gdvFormularios.DataKeys[rowIndex];

            int idForm = Convert.ToInt32(dtKey["ID"]);

            FormularioBO formularioBO = new FormularioBO();
            Formulario formulario = formularioBO.GetFormById(idForm);

            string AttachmentUrl = formulario.AttachmentUrl.Trim();
            string AttachmentName = formulario.AttachmentName;

            if (formulario.AttachmentName != "")
            {
                System.Web.HttpContext.Current.Session["FileName"] = AttachmentName;
                System.Web.HttpContext.Current.Session["Attachment"] = AttachmentUrl;
            }
            else
            {
                //Mandar mensagem de anexo não encontrado.
            }

            if (AttachmentUrl != string.Empty || AttachmentUrl != "")
            {
                Response.Redirect("download.aspx");
            }
        }
        protected void btnConsultarFormulario_Click(object sender, EventArgs e)
        {
            FilterReport();   
        }
        protected void btnLimparFormulario_Click(object sender, EventArgs e)
        {
            txtNumero.Text = string.Empty;
            txtNome.Text = string.Empty;
            txtMatricula.Text = string.Empty;
            ddlEstagio.SelectedValue = "0";
            SetGridView();
        }
        
        #endregion

    }
}
